# Copyright 2021 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_cmake_extra_content", "iree_runtime_cc_library")
load("//build_tools/bazel:iree_bitcode_library.bzl", "iree_bitcode_library")
load("//build_tools/embed_data:build_defs.bzl", "iree_c_embed_data")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

#===------------------------------------------------------------------------===#
# Common sources
#===------------------------------------------------------------------------===#

BITCODE_SRCS = [
    "device_generic.c",
]

BITCODE_HDRS = [
    "device.h",
]

#===------------------------------------------------------------------------===#
# Application-integrated static library
#===------------------------------------------------------------------------===#

iree_runtime_cc_library(
    name = "device",
    srcs = BITCODE_SRCS,
    hdrs = BITCODE_HDRS,
)

#===------------------------------------------------------------------------===#
# Compiler bitcode files
#===------------------------------------------------------------------------===#

iree_cmake_extra_content(
    content = """
if(NOT IREE_BUILD_COMPILER OR NOT IREE_TARGET_BACKEND_LLVM_CPU)
  return()
endif()
""",
    inline = True,
)

# TODO(benvanik): rule for building a matrix of bitcode files.

iree_bitcode_library(
    name = "libdevice_aarch64_sme",
    srcs = BITCODE_SRCS + ["arch/arm_64/sme_abi.c"],
    arch = "arm_64",
    internal_hdrs = BITCODE_HDRS,
)

iree_bitcode_library(
    name = "libdevice_wasm32_generic",
    srcs = BITCODE_SRCS,
    arch = "wasm_32",
    internal_hdrs = BITCODE_HDRS,
)

iree_bitcode_library(
    name = "libdevice_wasm64_generic",
    srcs = BITCODE_SRCS,
    arch = "wasm_64",
    internal_hdrs = BITCODE_HDRS,
)

iree_c_embed_data(
    name = "libdevice_bitcode",
    srcs = [
        ":libdevice_aarch64_sme.bc",
        ":libdevice_wasm32_generic.bc",
        ":libdevice_wasm64_generic.bc",
    ],
    c_file_output = "libdevice_bitcode.c",
    flatten = True,
    h_file_output = "libdevice_bitcode.h",
    identifier = "iree_builtins_libdevice_bitcode",
    deps = [
        "//runtime/src:runtime_defines",
    ],
)
